<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="父级id" prop="parentId">
         <el-tree-select
          v-model="queryParams.parentId"
          :data="cateTree"
          :props="defaultProps"
          check-strictly
          default-expand-all
          placeholder="请选择父类id"
          value-key="parentId"
        />
        <!-- <el-input v-model="queryParams.parentId" placeholder="请输入父级id" clearable @keyup.enter="handleQuery"
          class="!w-240px" /> -->
      </el-form-item>
      <el-form-item label="跳转链接" prop="url">
        <el-input v-model="queryParams.url" placeholder="请输入跳转链接" clearable @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="seo关键字" prop="seoTitle">
        <el-input v-model="queryParams.seoTitle" placeholder="请输入seo关键字" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="文件生成目录" prop="typeName">
        <el-input v-model="queryParams.typeName" placeholder="请输入文件生成目录" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="文章生成的链接" prop="siteurl">
        <el-input v-model="queryParams.siteurl" placeholder="请输入文章生成的链接" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="queryParams.sort" placeholder="请输入排序" clearable @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['cms:art-category:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['cms:art-category:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="success" plain @click="updateIndex">
          <Icon icon="ep:plus" class="mr-5px" /> 首页更新
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="分类名称" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="父级id" align="center" prop="parentId" />
      <el-table-column label="跳转链接" align="center" prop="url" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" />
      <el-table-column label="seo关键字" align="center" prop="seoKeywords" />
      <el-table-column label="seo关键字" align="center" prop="seoTitle" />
      <el-table-column label="seo描述" align="center" prop="seoDescription" />
      <el-table-column label="文件生成目录" align="center" prop="typeName" />
      <el-table-column label="文章生成的链接" align="center" prop="siteurl" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="列表模板 " align="center" prop="listTemplate" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="tz(scope.row.id)">更新</el-button>
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms:art-category:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['cms:art-category:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <iframe :src="url" v-show="show" scrolling="auto" frameborder="no" class="iframe"></iframe>

    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ArtCategoryForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="ArtCategory">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import {defaultProps, handleTree} from "@/utils/tree";
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as ArtCategoryApi from '@/api/cms/artCategory'
import ArtCategoryForm from './ArtCategoryForm.vue'
const message = useMessage() // 消息弹窗
const show = ref(false)
const cateTree = ref()
const { t } = useI18n() // 国际化
const url = ref()
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null,
  status: null,
  parentId: null,
  url: null,
  createTime: [],
  seoKeywords: null,
  seoTitle: null,
  seoDescription: null,
  typeName: null,
  siteurl: null,
  sort: null,
  listTemplate: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // console.log(getIntDictOptions(DICT_TYPE.COMMON_STATUS),'11111111111111111166')
    let obj = JSON.parse(JSON.stringify(queryParams))
    // console.log(obj,'11111111111111111155')
    if(obj.status != null){
      obj.status = obj.status == 1 ? 1 : 0
    }
    const data = await ArtCategoryApi.getArtCategoryPage(obj)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
// 首页更新
const updateIndex = ()=>{
  url.value = `http://101.200.84.135:48100/cms/index`
  show.value = true
  setTimeout(() => {
    show.value = false
  }, 1000)
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ArtCategoryApi.deleteArtCategory(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ArtCategoryApi.exportArtCategory(queryParams)
    download.excel(data, '文章分类.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
// const realHeight = ref(document.documentElement.clientHeight - 94.5 + "px;")


// 更新按钮
const tz = (id) => {
  url.value = `http://101.200.84.135:48100/cms/category?id=${id}`
  console.log(url.value)
  show.value = true
  setTimeout(() => {
    show.value = false
  }, 1000)

}
const getCate = async () => {
  cateTree.value = []
  const data = await ArtCategoryApi.getSimpleCateList()
  let dept: Tree = { id: 0, name: '顶级分类', children: [] }
  dept.children = handleTree(data)
  cateTree.value.push(dept)
}
/** 初始化 **/
onMounted(() => {
  getList()
  getCate()
  //   setTimeout(() => { loading.value = false; }, 100);
  // window.onresize = function () {
  //   realHeight.value = document.documentElement.clientHeight - 94.5 + "px;";
  // };

})
</script>



<style scoped lang="scss">
.iframe {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  background-color: white;
}
</style>
